package com.song.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.song.mapper.GameMapper;
import com.song.mapper.TeamMapper;
import com.song.pojo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**比赛的服务层
 * @author song
 */
@Service
public class GameService {
    @Resource
    private GameMapper gameMapper;
    @Resource
    private TeamService teamService;
    @Resource
    private GametypeService gametypeService;

    /**
     * 比赛的分页查询
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public PageInfo<Game> queryGames(Integer pageNum, Integer pageSize, GameQueryVO vo){
        GameExample example = new GameExample();
        GameExample.Criteria criteria = example.createCriteria();

        if (vo.getChineseName() != null&&!"".equals(vo.getChineseName().trim())){
            //再team中模糊查询，找到homeTeamId
            List<Integer> list = new ArrayList<>();
            TeamQueryVO teamQueryVO = new TeamQueryVO();
            teamQueryVO.setChineseName(vo.getChineseName());
            PageInfo<Team> pageInfo = teamService.queryTeams(null, null, teamQueryVO);
            for (Team team : pageInfo.getList()) {
                list.add(team.getTeamId());
            }
            criteria.andHomeTeamIdIn(list);
        }
        if (vo.getBeginDate() != null){
            criteria.andGameDateGreaterThanOrEqualTo(vo.getBeginDate());
        }
        if (vo.getEndDate() != null){
            criteria.andGameDateLessThanOrEqualTo(vo.getEndDate());
        }

        if (vo.getTypeId() != null&& vo.getTypeId() > 0){
            criteria.andTypeIdEqualTo(vo.getTypeId());
        }

        PageHelper.startPage(pageNum,pageSize);
        List<Game> games = gameMapper.selectByExample(example);

        //通过查询到的比赛主客队Id，找到主客队名称
        for (Game game : games) {
            //通过球队ID查询主队名称
            Team homeTeam = teamService.queryByTeamId(game.getHomeTeamId());
            game.setHomeTeamName(homeTeam.getChineseName());
            //通过球队ID查询克队名称
            Team visitingTeam = teamService.queryByTeamId(game.getVisitingTeamId());
            game.setVisitingTeamName(visitingTeam.getChineseName());
            //通过比赛类型ID查询比赛名称
            Gametype gametype = gametypeService.queryBytypeId(game.getTypeId());
            game.setGameType(gametype);
        }
        return new PageInfo<>(games);
    }

    /**
     * 比赛新增
     * @param game
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer insertGame(Game game){
        //新增比赛，设置未删除
        game.setIsDel(0);
        return gameMapper.insertSelective(game);
    }

    /**
     * 通过主键查询
     * @param gameId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Game queryByGameId(int gameId){
        return gameMapper.selectByPrimaryKey(gameId);
    }

    /**
     * 通过主键跟新
     * @param game
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer updateGame(Game game){
        return gameMapper.updateByPrimaryKeySelective(game);
    }
    /**
     * 通过主键删除
     * @param gameId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer deleteGame(Integer gameId){
        Game game = gameMapper.selectByPrimaryKey(gameId);
        game.setIsDel(1);
        return gameMapper.updateByPrimaryKeySelective(game);
    }

}
